package com.way.fileexplorer.adapters;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewGroup;

import com.way.fileexplorer.R;
import com.way.fileexplorer.activities.FileSearchResultActivity;
import com.way.fileexplorer.util.FileType;
import com.way.fileexplorer.util.IntentUtil;
import com.way.fileexplorer.util.NotifyManager;
import com.way.fileexplorer.util.Slog;

public class SearchListAdapter extends FileAdapter {
    private FileSearchResultActivity mContext;
    private boolean inSearchUi = true;
    private File mTopFile;
    private File mCurrentFile;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        ViewHolder vHolder = (ViewHolder) view.getTag(R.id.action_paste_mode_cancel);
        File file = mFileList.get(position);
        if (file.isDirectory()) {
            vHolder.fileIcon.setImageResource(R.drawable.file_item_folder_ic);
        } else {
            int fileType = FileType.getFileType(mContext).getFileType(file);
            view.setTag(R.id.viewtag_filetype, fileType);
            vHolder.fileIcon.setImageResource(fileType);
        }
        return view;
    }

    public SearchListAdapter(FileSearchResultActivity context) {
        super(context);
        mContext = context;
    }

    public void execute(int position) {
	if (mFileList == null){
		return;
	}
        File executeFile = mFileList.get(position);
        if (executeFile == null ) {
            return;
        }
        
        if (executeFile.isDirectory()) {
            try {
                refresh(executeFile);
            } catch (Exception e) {
                NotifyManager
                        .getInstance(mContext)
                        .showToast(
                                mContext.getString(R.string.fragment_search_invalid_result));
                Slog.e(e.toString());
                e.printStackTrace();
            }
        } else {
            int fileType = FileType.getFileType(mContext).getFileType(executeFile);
            Intent intent = IntentUtil.getIntentByFileType(mContext, fileType, executeFile);
            if(intent != null) {
                mContext.startActivity(intent);
            } else {
                NotifyManager.getInstance(mContext).showToast(mContext.getResources().getString(R.string.msg_invalid_intent));
            }
        }
    }

    public void pushToFileList(File file) {
        if(file.exists()){
            mFileList.add(file);
        }
    }

    public void clearFileList() {
        mFileList.clear();
    }
    
    public void refresh(File filePath){
        final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext);
        boolean displayHide = settings.getBoolean("display_hide_file", false);
        File fileArray[] = filePath.listFiles();
        List<File> fileList = Arrays.asList(fileArray);
        mFileList.clear();
        for(File file:fileList){
            if(displayHide){
                if(file.exists() && !mFileList.contains(file)){
                    mFileList.add(file);
                }
            }else{
                if(!file.isHidden() && file.exists() && !mFileList.contains(file)){
                    mFileList.add(file);
                }
            }
            
        }
        if(inSearchUi){
            mTopFile = filePath;
            inSearchUi = false;
        }
        mCurrentFile = filePath;
        startSort();
        notifyDataSetChanged();
    }
    
    /**
     * Go up level.
     * 
     * @return Return if finish activity.
     */
    public boolean popupFolder() {
        if(mTopFile != null && mCurrentFile != null){
            if (mTopFile.getPath().equals(mCurrentFile.getPath()) && !inSearchUi) {
                mContext.searchFile();
                inSearchUi = true;
                return false;
            }else if(!inSearchUi){
                File parentFile = mCurrentFile.getParentFile();
                refresh(parentFile);
                mCurrentFile = parentFile;
                return false;
            }else{
                return true;
            }
        }else{
            return true;
        }
        
    }
    
    public File getCurrentPath(){
        return mCurrentFile;
    }
    
    public boolean getInSearchUi(){
        return inSearchUi;
    }
}
